
import { createRouter, createWebHashHistory } from "vue-router";
import Layout from '../components/Layout.vue'
import About from '../view/About.vue'
import News from '../view/News.vue'
import Home from '../view/Home.vue'
import Login from '../view/Login.vue'
const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  { path: '/login', component: Login },
  {
    path: '/layout',
    component: Layout,
    redirect: '/layout/home',
    children: [
      { path: 'home', component: Home },
      { path: 'new', component: News },
      { path: 'about', component: About },
    ]
  },


]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('ele_login') ? true : false;
  if (to.path == '/layout') {
    isLogin ? next() : next('/login')
  } else {
    next();
  }
})
export default router